package cli.command;

import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.*;

public class Move implements Command{
    private PrintWriter out;
    private Path currentPath;

    public Move(PrintWriter out, Path currentPath) {
        this.out = out;
        this.currentPath = currentPath;
    }

    @Override
    public void handle(String[] args) {
        int pos = 0;
        if (pos >= args.length) {
            out.println("请输入源路径和目标路径！");
            return;
        }
        Path src = currentPath.resolve(args[pos++]);
        if (!Files.exists(src)) {
            out.println("源路径不存在！");
            return;
        }
        if (pos >= args.length) {
            out.println("请输入目标路径！");
            return;
        }
        Path dst = currentPath.resolve(args[pos++]);
        if (!Files.exists(dst.getParent())) {
            out.println("目标路径不存在！");
            return;
        }
        try {
            Files.move(src, dst, StandardCopyOption.ATOMIC_MOVE);
        } catch (FileAlreadyExistsException e) {
            out.println("同名文件已存在！");
        } catch (IOException e) {
            out.println("移动失败！");
        }
    }
}
